home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Mathematics / TeX / TeXit / Utilities / shiftmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  4.0 KB  |  140 lines

  1. /* 
  2.    Modifies a keymap to produce new shift-return, -tab and/or -delete
  3.    characters.  Note that the standard return key entry defines return
  4.    and command-return, we modify it to define return and shift-return.
  5.    This means that you lose the ability to produce command-return if 
  6.    you map shift-return.  The other key entries already specify shifted 
  7.    characters, so we just change the character produced.
  8.  
  9.    Compile with: cc -o shiftmap -O -s -object shiftmap.c
  10.    Usage: shiftmap options < /NextLibrary/Keyboards/USA.keymapping >
  11.       ~/Library/Keyboards/USAcustom.keymapping
  12.    Quit Preferences and restart it.  Select your new keymapping from
  13.    the keyboard item and the new keymapping will be installed.
  14.    I remapped my keyboard as shiftmap -d '|' -r '\' -t '`' < 
  15.    /NextLibrary/Keyboards/USA.keymapping > ~/Library/Keyboards/Mike.keymapping
  16.    
  17.    This code is public domain.  It is a quick hack that has worked for me,
  18.    but it surely isn't pretty.  Contact carlton@cs.berkeley.edu if you
  19.    have comments or suggestions.  It is known to work on the USA standard
  20.    keymapping, but may fail for the other keymappings.
  21.  
  22.    This program wouldn't be as much of a hack if I could find documentation 
  23.    on the format of keymappings.  This wouldn't even be necessary if the
  24.    Keyboard application from the 2.0 release did a more thorough job.  
  25.  
  26.    There is a hint that documentation does exist, but I don't know where
  27.    to get a copy of it.  Man 4 evs reveals this tantalizing tidbit:
  28.        "See the document ``Key Mappings on the NeXT Computer'' for more 
  29.      details on a key mapping string.  Key mappings are created by 
  30.      the KeyMap utility program."
  31.    If anyone can send either of these to me, I would be very grateful.
  32.  
  33.    To answer one question I've already gotten a couple of times: no, I
  34.    don't know how to disable or remap the brightness, sound or power keys.
  35.    I suspect that they may be in the same keymapping, but I did not 
  36.    completely decipher the keymapping format, and just don't know if they
  37.    are in there or not.
  38.  
  39.    Mar. 1, 1991
  40.    Mike Carlton    
  41. */
  42.  
  43. #include <stdio.h>
  44. #include <sys/types.h>
  45. #include <nextdev/keycodes.h>
  46.  
  47. #define SHIFT_ENTRY    stx
  48. #define CMD_ENTRY    eot
  49.  
  50. enum {
  51.     retrn,
  52.     tab,
  53.     delete,
  54.     num_maps
  55. };
  56.  
  57. char map[][4] = {
  58.     { CMD_ENTRY,   nul,  cr, nul },     /* Next char is cmd-return */
  59.     { SHIFT_ENTRY, nul,  ht, nul },     /* Next char is shift-tab */
  60.     { SHIFT_ENTRY, nul, del, nul }         /* Next char is shift-delete */
  61. };
  62.  
  63. char *name[] = {
  64.     "return", "tab", "delete"
  65. };
  66.  
  67. char newchar[num_maps];
  68.  
  69. void usage(char *program)
  70. {
  71.     fprintf(stderr, "Usage: %s [-r char] [-t char] [-d char]\n\n", program);
  72.     fprintf(stderr, 
  73.     "Reads a keymap on stdin and writes modified one to stdout.\n");
  74.     fprintf(stderr, 
  75.     "The options specify the new character to produce when shift\n");
  76.     fprintf(stderr, "return, tab or delete is pressed, respectively.\n");
  77. }
  78.  
  79.  
  80. int main(int argc, char *argv[])
  81. {
  82.     int i, j, bytes, changed = 0;
  83.     char c, buf[4096], *key, *lim;
  84.     extern int optind;
  85.     extern char *optarg;
  86.  
  87.     if (argc == 1) {
  88.         usage(argv[0]);
  89.         exit(1);
  90.     }
  91.  
  92.         while ((c = getopt(argc, argv, "r:t:d:")) != EOF)
  93.             switch (c) {
  94.                     case 'r':
  95.             newchar[retrn] = optarg[0];
  96.                         break;
  97.                     case 't':
  98.             newchar[tab] = optarg[0];
  99.                         break;
  100.                     case 'd':
  101.             newchar[delete] = optarg[0];
  102.                         break;
  103.             default:
  104.             usage(argv[0]);
  105.             exit(1);
  106.             break;
  107.         }
  108.  
  109.  
  110.     bytes = fread(buf, sizeof(char), 1024, stdin);
  111.     key = buf;
  112.     lim = key+bytes;
  113.  
  114.     while (key < lim-4) {
  115.         for (i = 0; i < num_maps; i++)
  116.             if (newchar[i]) {
  117.                 for (j = 0; j < 4; j++)
  118.                     if (key[j] != map[i][j])
  119.                         break;
  120.  
  121.                 if (j == 4) {       /* we matched */
  122.                     key[0] = SHIFT_ENTRY;  /* for return */
  123.                     key[4] = newchar[i];
  124.                     fprintf(stderr, "Remapping shift-%s\n",
  125.                         name[i]);
  126.                     changed++;
  127.                     break;
  128.                 }
  129.             } 
  130.         key++;
  131.     }
  132.  
  133.     if (!changed) 
  134.         fprintf(stderr, "No keys found to remap\n");
  135.  
  136.     fwrite(buf, sizeof(char), bytes, stdout);
  137.  
  138.     return 0;
  139. }
  140.